package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskManageDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskManage;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for TaskManage entities
 * 
 */

@Service("TaskManageService")

@Transactional
public class TaskManageServiceImpl implements TaskManageService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskManage entities
	 * 
	 */
	@Autowired
	private TaskManageDAO taskManageDAO;

	/**
	 * Instantiates a new TaskManageServiceImpl.
	 *
	 */
	public TaskManageServiceImpl() {
	}

	/**
	 * Return a count of all TaskManage entity
	 * 
	 */
	@Transactional
	public Integer countTaskManages() {
		return ((Long) taskManageDAO.createQuerySingleResult("select count(o) from TaskManage o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing TaskManage entity
	 * 
	 */
	@Transactional
	public Set<TaskManage> loadTaskManages() {
		return taskManageDAO.findAllTaskManages();
	}

	/**
	 * Return all TaskManage entity
	 * 
	 */
	@Transactional
	public List<TaskManage> findAllTaskManages(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<TaskManage>(taskManageDAO.findAllTaskManages(startResult, maxRows));
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public TaskManage saveTaskManageEmployee(Integer id, Employee related_employee) {
		TaskManage taskmanage = taskManageDAO.findTaskManageByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		taskmanage.setEmployee(related_employee);
		related_employee.getTaskManages().add(taskmanage);
		taskmanage = taskManageDAO.store(taskmanage);
		taskManageDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return taskmanage;
	}

	/**
	 * Save an existing TaskManage entity
	 * 
	 */
	@Transactional
	public void saveTaskManage(TaskManage taskmanage) {
		TaskManage existingTaskManage = taskManageDAO.findTaskManageByPrimaryKey(taskmanage.getId());

		if (existingTaskManage != null) {
			if (existingTaskManage != taskmanage) {
				existingTaskManage.setId(taskmanage.getId());
				existingTaskManage.setRemind(taskmanage.getRemind());
			}
			taskmanage = taskManageDAO.store(existingTaskManage);
		} else {
			taskmanage = taskManageDAO.store(taskmanage);
		}
		taskManageDAO.flush();
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public TaskManage deleteTaskManageTask(Integer taskmanage_id, Integer related_task_id) {
		TaskManage taskmanage = taskManageDAO.findTaskManageByPrimaryKey(taskmanage_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		taskmanage.setTask(null);
		related_task.getTaskManages().remove(taskmanage);
		taskmanage = taskManageDAO.store(taskmanage);
		taskManageDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return taskmanage;
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public TaskManage deleteTaskManageEmployee(Integer taskmanage_id, Integer related_employee_id) {
		TaskManage taskmanage = taskManageDAO.findTaskManageByPrimaryKey(taskmanage_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		taskmanage.setEmployee(null);
		related_employee.getTaskManages().remove(taskmanage);
		taskmanage = taskManageDAO.store(taskmanage);
		taskManageDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return taskmanage;
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public TaskManage saveTaskManageTask(Integer id, Task related_task) {
		TaskManage taskmanage = taskManageDAO.findTaskManageByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		taskmanage.setTask(related_task);
		related_task.getTaskManages().add(taskmanage);
		taskmanage = taskManageDAO.store(taskmanage);
		taskManageDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return taskmanage;
	}

	/**
	 * Delete an existing TaskManage entity
	 * 
	 */
	@Transactional
	public void deleteTaskManage(TaskManage taskmanage) {
		taskManageDAO.remove(taskmanage);
		taskManageDAO.flush();
	}

	/**
	 */
	@Transactional
	public TaskManage findTaskManageByPrimaryKey(Integer id) {
		return taskManageDAO.findTaskManageByPrimaryKey(id);
	}
}
